define(function (require, exports, module) {
    var FXREADER = require('./FXReader.js');
    FXREADER.HTML5 = {};
FXREADER.HTML5.PDFPage = function(pdfApp,index)
{
	var _pdfApp = pdfApp,
		_nPageIndex = index;

	this.GetPageIndex = function()
	{
		return _nPageIndex;
	};
	this.GetPageWidth = function(){
		return _pdfApp.GetPageSize(_nPageIndex)[0];
	};

	this.GetPageHeight = function()
	{
		return _pdfApp.GetPageSize(_nPageIndex)[1];
	};
	
	this.GetPDFApp = function(){
		return _pdfApp;
	};
};


FXREADER.HTML5.CanvasPageViewRender = function()
{
	
	var _PageInRenderList = new Array();
	this.RenderPage = function (pPageView, retryCount)
	{
		if (pPageView == null || !pPageView.IsContentCreated() || pPageView.IsThumb())
            return;
        if (pPageView.IsPageLoaded()) {
            return;
        }
	        
	        //console.log("此时需要进来的view是第" + pPageView.GetPageIndex() + "页");
	        if(_PageInRenderList.length == 0){
	    		  _PageInRenderList.push(pPageView);
	    		  //console.log("数组为空,添加第" + pPageView.GetPageIndex() + "页数据,需要进行绘制");
	        }else{
	        	var len = _PageInRenderList.length;
	        	var flag = false;
	        	for(var i = 0;i < len;i++){
	        		var pageviewtmp = _PageInRenderList[i];
	        		//数组中是否包含该页数据
	        		if(pageviewtmp.GetPageIndex() == pPageView.GetPageIndex()){//包含该页数据
	        			flag = true;
	        			//判断跟数组中的该页数据是否是一样的 
	        			if(pageviewtmp.GetPageViewWidth() != pPageView.GetPageViewWidth()){
		  	    		  	_PageInRenderList.push(pPageView);
		  	    		  	//console.log("数组中包含第" + pPageView.GetPageIndex() + "页数据,并且宽不一样,需要重新绘制");
	        			}else{
	        				//console.log("数组中包含第" + pPageView.GetPageIndex() + "页数据,但宽一样,不需要重新绘制");
	        			}
	        			break;
	        		}
	        	}
	        	
	        	if(!flag){
	        		//console.log("数组中没有包含第" + pPageView.GetPageIndex() + "页数据,需要绘制");
  	    		  	_PageInRenderList.push(pPageView);
	        	}
	        }
	        
	        var timer = setInterval(function(){
	        	if(_PageInRenderList.length == 0){
	        		_now = 0;
	        		//console.log("清除循环调用");
	        		clearInterval(timer);
	        	}else{
	        		for(var i  = 0;i < _PageInRenderList.length;i++){
	        			var currPageView = _PageInRenderList[i];
		        		var pdfPage = currPageView.GetPDFPage();
		    	        var drawPageStatus = pdfPage.GetPDFApp().GetDrawPageStatus();
		    	        if(drawPageStatus == 0){
		    	        	render(currPageView);
		    	        }else if(drawPageStatus == 3){
		    	        	currPageView.SetPageLoaded(true);
		    	        	pdfPage.GetPDFApp().SetDrawPageStatus(0);
		    	        	var len = _PageInRenderList.length;
		    	        	_PageInRenderList = _PageInRenderList.slice(0,i).concat(_PageInRenderList.slice(i+1,_PageInRenderList.length));//删除该元素
		    	        	var width = parseInt(currPageView.GetPageViewWidth());//判断该页的宽
		    	        	var canvaswidth = $("#canvas" + currPageView.GetPageIndex()).width();//判断canvas的宽
		    	        	if(width != canvaswidth && canvaswidth != null){//对没有反应的canvas进行重绘
		    	        		render(currPageView);
		    	        	}
		    	        	//console.log("清除前的长度:" + len + ",清除后的长度:" + _PageInRenderList.length + ",清除第" + currPageView.GetPageIndex() + "页绘制完成");
		    	        	break;
		    	        }else if(drawPageStatus == 1){
		    	        	
		    	        }
	        		}
	        	}
	        },500);
     
	        function render(pPageView){
				var pageIndex = pPageView.GetPageIndex();
				//console.log("第" + pageIndex + "页进行绘制");
				var pageContainerID = pPageView.GetWrapPageID();
				var domPageDiv = document.getElementById(pageContainerID);
		        var pageWidth = pPageView.GetPageViewWidth();
				var pageHeight = pPageView.GetPageViewHeight();
				var pdfPage = pPageView.GetPDFPage();
				pdfPage.GetPDFApp().RenderPage(pageIndex,domPageDiv,0,0,pageWidth,pageHeight,0);
				if(pdfPage.GetPDFApp().GetDrawPageStatus()){
					$("#" + pPageView.GetPageLoaderImgID()).hide();
				}
	        }
	};
	this.GetPixelsPerPoint = function () {
        return 96 / 72;
    };
};

FXREADER.HTML5.PDFDocument = function(pdfApp)
{
	var _pdfApp = pdfApp,
		_pdfPageList = [],
		_self = this,
		_StartShowPage = 0,      //限制可以显示的开始页
        _EndShowPage = -1;
	var _pageCount = _pdfApp.GetPageCount();

	this.GetPageCount = function(){
		return _pdfApp.GetPageCount();
	};

	this.InitPagesList = function(){
		for(var i = 0; i < _pageCount; i++){
			var PDFPage = new FXREADER.HTML5.PDFPage(_pdfApp,i);
			_pdfPageList.push(PDFPage);
		}
		
	};
	//返回IPDFPage接口
	this.GetPage = function(pageIndex){
		return _pdfPageList[pageIndex];
	};
	this.SetShowPageRange = function (nStartShowPage, nEndShowPage) { _StartShowPage = nStartShowPage; _EndShowPage = nEndShowPage; };
};

FXREADER.HTML5.DocumentLoader = function(docId,docUrl,pageUrl,dataUrl)
{

	var _docId = docId,
		_PDFApp = null,
		_pdfDoc = null,
		_self = this,
		_docUrl = docUrl,
		_pageUrl = pageUrl,
		_dataUrl = dataUrl;

	//内部初始化
	function _Init()
	{
		if(_PDFApp == null)
		{
			_PDFApp = new ViewSDK(_docId,_docUrl,_pageUrl,_dataUrl);
		}
	}

	//异步加载PDF文档，提供回调,回调参数 [IPDFDocument,status,errMsg]
	this.AysncLoadDocument = function(callback)
	{
		_Init();
		_PDFApp.OpenPDDocument(function(status){
			if(status == "success")
			{
				_pdfDoc = new FXREADER.HTML5.PDFDocument(_PDFApp);
				_pdfDoc.InitPagesList();
				callback(_pdfDoc,status);
			}
		});
	};
	};
});
